Partial Class SB_broadcastMessage
    Inherits System.Web.UI.Page

    Protected Sub SendButton_Click(ByVal sender As Object, _
                                   ByVal e As System.EventArgs) _
                                   Handles SendButton.Click

        Dim messageSaved As Boolean = False
        Dim messageRoleSaved As Boolean = False
        Dim showFailedUsers As Boolean = False
        Dim emptyEmailUsers As New Hashtable
        Dim emptyKey As Integer = 1
        Dim user As Cdm.DataCore.UserSecurity.CDMUser
        Dim currentUser As Cdm.DataCore.UserSecurity.CDMUser
        currentUser = UserAuthorization.CurrentUser
        Dim messageText As String
        Dim senderInfo As String

        senderInfo = String.Concat("Sender:  ", currentUser.FirstName, " ", currentUser.LastName)
        If Not String.IsNullOrEmpty(currentUser.EmailAddress) Then
            senderInfo = String.Concat(senderInfo, vbCrLf, "Email Address: ", currentUser.EmailAddress)
        End If

        messageText = String.Concat("This is a Broadcast message from the ", Application("AppName"), " Application.", vbCrLf, vbCrLf, _
        senderInfo, vbCrLf, vbCrLf, _
        Me.MessageTextBox.Text)


        ' Save our broadcast message to the database.
        Dim broadcastMessage As Cdm.DataCore.Custom.BroadcastMessage
        Dim broadcastMessageRole As Cdm.DataCore.Custom.BroadcastMessageRole

        ' Catch any failed saves.  This will cause the email to NOT be sent out.
        Try

            broadcastMessage = New Cdm.DataCore.Custom.BroadcastMessage(currentUser.UserId, _
                                                                        Now, _
                                                                        Me.SubjectTextBox.Text, _
                                                                        messageText)
            broadcastMessage.Persist()
            messageSaved = True

        Catch ex As Exception

            broadcastMessage = Nothing
            messageSaved = False

        End Try

        If messageSaved Then

            ' Loop through roles.
            For Each item As ListItem In Me.RoleCheckBoxList.Items

                ' Determine if role is selected.
                If item.Selected Then

                    If broadcastMessage IsNot Nothing Then
                        ' Save each broadcast message role to the database.
                        broadcastMessageRole = New Cdm.DataCore.Custom.BroadcastMessageRole(broadcastMessage.Id, _
                                                                                            item.Value)
                        broadcastMessageRole.Persist()
                    End If

                    ' Loop through users listed by role.
                    For Each user In Cdm.DataCore.UserSecurity.CDMUser.ListByRoleId(CInt(item.Value))

                        If user.EmailAddress <> "" Then

                            ' Send an email to each user.
                            If currentUser.EmailAddress <> "" Then

                                Cdm.AppServices.SendEmail.Send(Application("EmailServer"), _
                                                               Application("EmailFromAddress"), _
                                                               Application("EmailFromDisplayName"), _
                                                               currentUser.EmailAddress, _
                                                               currentUser.FirstName & " " & currentUser.LastName, _
                                                               user.EmailAddress, _
                                                               Me.SubjectTextBox.Text, _
                                                               messageText)

                            Else

                                Cdm.AppServices.SendEmail.Send(Application("EmailServer"), _
                                                               Application("EmailFromAddress"), _
                                                               Application("EmailFromDisplayName"), _
                                                               user.EmailAddress, _
                                                               Me.SubjectTextBox.Text, _
                                                               messageText)

                            End If

                        Else

                            showFailedUsers = True
                            emptyEmailUsers.Add(emptyKey, user.FirstName & " " & user.LastName)
                            emptyKey += 1

                        End If

                    Next

                    item.Selected = False

                End If

            Next

            MessageLabel.Visible = True
            If showFailedUsers Then

                ' List all users that have no email address in the system.
                MessageLabel.Text = "The following user(s) do not have a valid email address listed and were not emailed:<br>"
                For i As Integer = 0 To emptyEmailUsers.Count - 1
                    MessageLabel.Text &= emptyEmailUsers.Item(i) & "<br>"
                Next

            Else

                MessageLabel.Text = "Message sent successfully"
                Me.MessageTextBox.Text = ""
                Me.SubjectTextBox.Text = ""

            End If

        Else

            MessageLabel.Visible = True
            MessageLabel.Text = "This message was not successfully saved to the database and will NOT be sent out via email."

        End If

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            If EntityController.IsSingleParticipantUser() Or EntityController.IsGuestUser() Then
                Response.Redirect("~/default.aspx")
            End If
        End If
    End Sub

End Class
